﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Dora.Tools.Utility
{
    /// <summary>
    /// 枚举工具类
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 返回枚举的Description
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string ToDescription(this System.Enum e)
        {
            FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
            if (fieldInfo != null)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes != null && attributes.Any())
                {
                    return attributes.FirstOrDefault().Description;
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// 返回枚举的Description 
        /// </summary>
        /// <param name="enumSubitem"></param>
        /// <returns></returns>
        public static string GetEnumDescription(this System.Enum enumSubitem)
        {
            string strValue = enumSubitem.ToString();

            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
            if (fieldinfo == null)
                return null;

            Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (objs == null || objs.Length == 0)
            {
                return strValue;
            }
            else
            {
                DescriptionAttribute da = (DescriptionAttribute)objs[0];
                return da.Description;
            }
        }

        /// <summary>
        /// 根据枚举值获取枚举描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertEnumTextByValue<T>(int value)
        {
            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                System.Enum outObj = System.Enum.Parse(typeof(T), item.ToString()) as System.Enum;
                if (value == Convert.ToInt32(outObj))
                {
                    return outObj.GetEnumDescription();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取枚举值的集合信息
        /// </summary>
        /// <returns></returns>
        public static List<KeyValueEntity> GetEnumDescriptionList<T>()
        {
            var result = new List<KeyValueEntity>();
            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                System.Enum outObj = System.Enum.Parse(typeof(T), item.ToString()) as System.Enum;
                result.Add(new KeyValueEntity()
                {
                    Key = Convert.ToInt32(outObj),
                    Description = outObj.GetEnumDescription()
                });
            }
            return result;
        }

        /// <summary>
        /// 获取枚举的值和描述集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<SelectListItem> GetEnumSelectListItems<T>()
        {
            List<SelectListItem> sources = new List<SelectListItem>();

            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                System.Enum outObj = System.Enum.Parse(typeof(T), item.ToString()) as System.Enum;
                sources.Add(new SelectListItem
                {
                    Text = outObj.GetEnumDescription(),
                    Value = Convert.ToInt32(outObj).ToString()
                });
            }

            return sources;
        }

        /// <summary>
        /// 实体集合转换
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<SelectListItem> TransformList(List<KeyValueEntity> source)
        {
            var returnResult = new List<SelectListItem>();
            if (source != null && source.Count > 0)
            {
                source.ForEach(p =>
                {
                    returnResult.Add(new SelectListItem() { Text = p.Description, Value = p.Key.ToString() });
                });
            }
            return returnResult;
        }

        /// <summary>
        /// 获取枚举值的集合信息,排除值为0的
        /// </summary>
        /// <returns></returns>
        public static List<KeyValueEntity> GetEnumDescriptionListNotZero<T>()
        {
            var result = new List<KeyValueEntity>();
            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                System.Enum outObj = System.Enum.Parse(typeof(T), item.ToString()) as System.Enum;
                if (Convert.ToInt32(outObj) != 0)
                {
                    result.Add(new KeyValueEntity()
                    {
                        Key = Convert.ToInt32(outObj),
                        Description = outObj.GetEnumDescription()
                    });
                }
            }
            return result;
        }

        /// <summary>
        /// 获取枚举的值和描述集合,排除值为0的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<SelectListItem> GetEnumSelectListItemsNotZero<T>()
        {
            List<SelectListItem> sources = new List<SelectListItem>();

            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                System.Enum outObj = System.Enum.Parse(typeof(T), item.ToString()) as System.Enum;
                if (Convert.ToInt32(outObj) != 0)
                {
                    sources.Add(new SelectListItem
                    {
                        Text = outObj.GetEnumDescription(),
                        Value = Convert.ToInt32(outObj).ToString()
                    });
                }
            }

            return sources;
        }
    }


    public class KeyValueEntity
    {
        /// <summary>
        /// 值
        /// </summary>
        public int Key { get; set; }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; }
    }

    public class SelectListItem
    {
        public bool Selected { get; set; }
        public string Text { get; set; }
        public string Value { get; set; }
    }
}
